package mobi.bantu.service.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import mobi.bantu.bean.Cons;
import mobi.bantu.bean.Operations;
import mobi.bantu.bean.Result;
import mobi.bantu.bean.User;
import mobi.bantu.bean.task.Report;
import mobi.bantu.bean.task.ReportLog;
import mobi.bantu.dao.mapper.ReportLogMapper;
import mobi.bantu.dao.mapper.ReportMapper;
import mobi.bantu.dao.mapper.UserMapper;
import mobi.bantu.service.TaskService;
import mobi.bantu.utils.DateUtils;
import mobi.bantu.utils.NumUtils;

@Service
@Transactional
public class TaskServiceImpl implements TaskService {

    @Resource
    private ReportMapper reportMapper;
    @Resource
    private ReportLogMapper reportLogMapper;
    @Resource
    private UserMapper userMapper;

    @Override
    public Result report(Report report) {
        reportMapper.save(report);
        return Result.success();
    }

    @Override
    public List<Report> listWait(Long userId) {
        Map<String, Object> param = new HashMap<>();
        param.put("userId", userId);
        param.put("stas", new int[]{0, 1});
        return reportMapper.list(param);
    }

    @Override
    public List<Report> listOver(Long userId) {
        Map<String, Object> param = new HashMap<>();
        param.put("userId", userId);
        param.put("stas", new int[]{2, 9});
        return reportMapper.list(param);
    }

    @Override
    public List<Report> waitSure() {
        Map<String, Object> param = new HashMap<>();
        param.put("status", Operations.WAIT_SURE.code);
        return reportMapper.list(param);
    }

    @Override
    public Report getById(Long reportId) {
        return reportMapper.getById(reportId);
    }

    @Override
    public List<Report> overMaintain() {
        Map<String, Object> param = new HashMap<>();
        param.put("status", Operations.MAINTAIN.code);
        return reportMapper.list(param);
    }

    @Override
    public List<Report> getByMainId(Long id, int[] stas) {
        Map<String, Object> param = new HashMap<>();
        param.put("mainId", id);
        param.put("stas", stas);
        return reportMapper.list(param);
    }

    @Override
    public Result sure(User admin, Long reportId, Long mainId) {
        Report report = reportMapper.getById(reportId);
        if (report == null) return Result.byMsg("参数错误");
        if (report.getStatus() != Cons.INIT) return Result.byMsg("状态错误");
        User mainUser = userMapper.getById(mainId);
        if (mainUser == null) return Result.byMsg("维修人员不存在");
        Date now = new Date();
        report.setMainId(mainId);
        Operations operation = Operations.MANAGER_SURE;
        report.setStatus(operation.code);
        report.setRemark(DateUtils.formatDate(now) + operation.name);
        reportMapper.update(report);

        ReportLog log = new ReportLog();
        log.setUserId(admin.getId());
        log.setCreateTime(now);
        log.setOperation(operation);
        log.setReportId(reportId);
        log.setStatus(1);
        log.setHanderName(admin.getRealname());
        log.setHanderMobile(admin.getMobile());
        log.setUsername(admin.getRealname());
        log.setRemark(String.format("%s:指定:%s,%s处理", admin.getRealname(), mainUser.getRealname(), mainUser.getMobile()));
        reportLogMapper.insert(log);
        return Result.success();
    }

    @Override
    public Result delete(User user, Long reportId) {
        Report report = reportMapper.getById(reportId);
        if (report == null) return Result.byMsg("参数错误");
        if (report.getStatus() != Cons.INIT) return Result.byMsg("状态错误");
        if (!NumUtils.eq(report.getUserId(), user.getId())) return Result.byMsg("错误操作");
        Date now = new Date();
        Operations operation = Operations.DELETE;
        reportMapper.delete(reportId);
        ReportLog log = new ReportLog();
        log.setUserId(user.getId());
        log.setCreateTime(now);
        log.setOperation(operation);
        log.setReportId(reportId);
        log.setStatus(1);
        log.setRemark("提交人员删除");
        log.setUsername(user.getRealname());
        reportLogMapper.insert(log);
        return Result.success();
    }

    @Override
    public Result toMyOver(User mainUser, Long reportId) {
        Report report = reportMapper.getById(reportId);
        if (report == null) return Result.byMsg("参数错误");
        if (report.getStatus() != 1) return Result.byMsg("状态错误");
        if (mainUser == null || !mainUser.isWork() || !NumUtils.eq(report.getMainId(), mainUser.getId()))
            return Result.byMsg("维修人员不正确");
        Date now = new Date();
        Operations operation = Operations.MAINTAIN;
        report.setStatus(operation.code);
        report.setRemark(DateUtils.formatDate(now) + operation.name);
        reportMapper.update(report);

        ReportLog log = new ReportLog();
        log.setUserId(mainUser.getId());
        log.setCreateTime(now);
        log.setOperation(operation);
        log.setReportId(reportId);
        log.setStatus(1);
        log.setHanderName(mainUser.getRealname());
        log.setHanderMobile(mainUser.getMobile());
        log.setUsername(mainUser.getRealname());
        log.setRemark(mainUser.getRealname() + ":维修完成");
        reportLogMapper.insert(log);
        return Result.success();
    }

    @Override
    public Result toOver(User admin, Long reportId) {
        Report report = reportMapper.getById(reportId);
        if (report == null) return Result.byMsg("参数错误");
        if (report.getStatus() != 2) return Result.byMsg("状态错误");
        Date now = new Date();
        Operations operation = Operations.MAINTAIN_OVER;
        report.setStatus(operation.code);
        report.setRemark(DateUtils.formatDate(now) + operation.name);
        reportMapper.update(report);
        ReportLog log = new ReportLog();
        log.setUserId(admin.getId());
        log.setCreateTime(now);
        log.setOperation(operation);
        log.setReportId(reportId);
        log.setStatus(1);
        log.setHanderName(admin.getRealname());
        log.setHanderMobile(admin.getMobile());
        log.setUsername(admin.getRealname());
        log.setRemark(admin.getRealname() + ":确认办结");
        reportLogMapper.insert(log);
        return Result.success();
    }

    @Override
    public Result evalute(User user, Long reportId, String evalute) {
        Report report = reportMapper.getById(reportId);
        if (report == null) return Result.byMsg("参数错误");
        if (!report.isOver()) return Result.byMsg("状态错误");
        if (StringUtils.isNotBlank(report.getEvaluate())) return Result.byMsg("已评价");
        if (!NumUtils.eq(report.getUserId(), user.getId())) return Result.byMsg("操作有误");
        Date now = new Date();
        report.setEvaluate(evalute);
        report.setRemark(DateUtils.formatDate(now) + "评价");
        reportMapper.update(report);

        ReportLog log = new ReportLog();
        log.setUserId(user.getId());
        log.setCreateTime(now);
        log.setOperation(Operations.EVALUTE);
        log.setReportId(reportId);
        log.setStatus(1);
        log.setUsername(user.getRealname());
        log.setRemark(user.getRealname() + ":已评价");
        reportLogMapper.insert(log);
        return Result.success();
    }

    @Override
    public List<Report> waitMain() {
        Map<String, Object> param = new HashMap<>();
        param.put("status", Operations.MANAGER_SURE.code);
        return reportMapper.list(param);
    }

    @Override
    public List<Report> overList() {
        Map<String, Object> param = new HashMap<>();
        param.put("status", Operations.MAINTAIN_OVER.code);
        return reportMapper.list(param);
    }

    @Override
    public List<Report> list(Map<String, Object> param) {
        return reportMapper.list(param);
    }

    @Override
    public int count(Map<String, Object> param) {
        return reportMapper.count(param);
    }
}
